package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.java.dao.LoginDao;
import com.java.ext.MyButton;
import com.java.utils.MyWindowListener;  

/**
* @ClassName: AdminPasswordWindow
* @Description: 管理员修改密码
*
 */
public class AdminPasswordWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 父级窗口
	 */
	JFrame pJFrame;

	// 构造函数  
    public AdminPasswordWindow(JFrame bJFrame) {
    	pJFrame = bJFrame;
        // 设置窗口属性  
        setTitle("Administrator password modification");  
		setSize(350, 168);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
        addWindowListener(new CloseButtonListener()); 
        setLocationRelativeTo(null);
  
        // 创建用于布局的 JPanel  
        JPanel contentPane = new JPanel();  
        contentPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); 
        contentPane.setLayout(new GridLayout(3, 2)); // 使用网格布局管理器  
  
        // 创建密码输入框  
        final JTextField oldPsw = new JTextField();  
        contentPane.add(new JLabel("Old password: "), new GridBagConstraints());  
        contentPane.add(oldPsw, new GridBagConstraints());  
  
        // 创建密码输入框  
        final JTextField newPsw = new JTextField();  
        contentPane.add(new JLabel("New password: "), new GridBagConstraints());  
        contentPane.add(newPsw, new GridBagConstraints());  
  
        // 创建密码输入框  
        final JTextField newPsw2 = new JTextField();  
        contentPane.add(new JLabel("New password: "), new GridBagConstraints());  
        contentPane.add(newPsw2, new GridBagConstraints());  
  
  
        // 创建重置按钮和提交按钮的面板  
        JPanel buttonPanel = new JPanel();  
        buttonPanel.setLayout(new FlowLayout()); // 使用流式布局，使按钮水平排列  
        MyButton saveBtn = new MyButton("Confirm modifications").primary();
        buttonPanel.add(saveBtn);  
  
        // 将按钮面板添加到主窗口的底部  
        getContentPane().add(contentPane, BorderLayout.CENTER);  
        getContentPane().add(buttonPanel, BorderLayout.SOUTH); // 添加到底部 
		
        saveBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	String oldPswStr = oldPsw.getText(); 
            	String newPswStr = newPsw.getText();
            	String newPswStr2 = newPsw2.getText();
            	if("".equals(oldPswStr)) {
            		warning("Please enter the original password");
            		return;
            	}
            	if(!oldPswStr.equals(session.getUserPassword())) {
            		warning("Old password error");
            		return;
            	}
            	if("".equals(newPswStr) || "".equals(newPswStr2)) {
            		warning("Please enter a new password");
            		return;
            	}
            	if(!newPswStr.equals(newPswStr2)) {
            		warning("Two passwords do not match");
            		return;
            	}
            	LoginDao dao = new LoginDao();
            	session.setUserPassword(newPswStr2);
            	dao.updateAdminPsw(session.getUserId(), newPswStr2);
            	ok("Success");
            }  
        }); 
        
		setVisible(true);
		
    } 
    
    class CloseButtonListener extends MyWindowListener {
    	
    	@Override
    	public void windowClosing(WindowEvent e) {  
        	setVisible(false);
        	pJFrame.setEnabled(true);
        }
    }
      
}